//global for size of board
var gamesize = 10;
var blueInitial = new Array();
var greenInitial = new Array();
var redInitial = new Array();
var yellowInitial = new Array();
var purpleInitial = new Array();
var orangeInitial = new Array();
function generateBoard()
{
	for (var i = 0; i < gamesize; i++)
	{//ycoord
		for (var j = 0; j < gamesize; j++)
		{//xcoord
			makediv(i,j);	
		}
	}
}
function makediv(i,j)
{
	var board = document.getElementById("gameboard");
	var newdiv = document.createElement("div");
	var theID = '#p' + j + i;
	newdiv.id = 'p' + j + i;
	var random = Math.floor(Math.random()*6);
	switch(random)
	{
		// build initial color arrays to be used, contain all elements of a color
		// give class names
		case 0:
			newdiv.className = 'pieces green';
			greenInitial.push(theID);
			break;
		case 1:
			newdiv.className = 'pieces purple';
			purpleInitial.push(theID);
			break;
		case 2:
			newdiv.className = 'pieces yellow';
			yellowInitial.push(theID);
			break;
		case 3:
			newdiv.className = 'pieces red';
			redInitial.push(theID);
			break;
		case 4:
			newdiv.className = 'pieces orange';
			orangeInitial.push(theID);
			break;
		case 5:
			newdiv.className = 'pieces blue';
			blueInitial.push(theID);
			break;
		default:
			return;
	}
	board.appendChild(newdiv);
}
/***********************************************
*********************end build board************ 
***********************************************/
/*
Flood-fill (node, target-color, replacement-color):
1. If the color of node is not equal to target-color, return.
2. Set the color of node to replacement-color.
3. Perform Flood-fill (one step to the west of node, target-color, replacement-color).
	Perform Flood-fill (one step to the east of node, target-color, replacement-color).
	Perform Flood-fill (one step to the north of node, target-color, replacement-color).
	Perform Flood-fill (one step to the south of node, target-color, replacement-color).
4. Return.
*/
function getnextids(ID, index)
{
	var nexts = new Array();
	var tempID = ID.replace('p', '');
	var xindex = tempID[0];
	var yindex = tempID[1];
	var temp = 0, newID = '';

		// right of current
		temp = xindex + 1;
		newID = 'p' + temp + yindex;
		nexts.push(newID);
		//left of current
		temp = xindex - 1;
		newID = 'p' + temp + yindex;
		nexts.push(newID);
		//below current
		temp = yindex + 1;
		newID = 'p' + xindex + temp;
		nexts.push(newID);
		//above current
		temp = yindex - 1;
		newID = 'p' + xindex + temp;
		return nexts;
}

$(document).ready( function() {
	generateBoard();
	$('#gameboard div').click(function ()
	{
		var clicked = $(this).attr('id');
		piecesaround(clicked);
	});
	$('#bluecont').click(function ()
	{
		move('blue');
	});
	$('#redcont').click(function ()
	{
		move('red');
	});
	$('#greencont').click(function ()
	{
		move('green');
	});
	$('#yellowcont').click(function ()
	{
		move('yellow');
	});
	$('#purplecont').click(function ()
	{
		move('purple');
	});
	$('#orangecont').click(function ()
	{
		move('orange');
	});

});
/*check to see if initial first element touches colors already!
//push element p00 onto an array of joined pieces done* (add 'done' to class)
//when creating pieces, make a list of colors as theyre built to use later
//wait for input
//	get colorInput array for the color input (all have class *color* in common)
//	change done*'s color class name to new input color
//	check all elements in colorInput array
//		see if they touch elements in done*
		if they do, add them to done* 
		remove elements you added to done* from initial added color array
		put those elements into a  temp array,  
		call a function with that temp and the newly updated initial color array,
			if temp elements touch initial color array elements
			repeat
				add the initial color arr elements that touch to done*
				remove them from initial color
				delete temp, set it equal to ones from above
				call again on temp and initial color arr
			until no more matches 
			this may not be needed if done.length updates, and checks every single element that was
		added too???
		if done.length == #ofpcs, they win
		else
			if #ofMoves !< #allowed, game over
			else play on.*/
function move(inputColor)
{
	updateDone();
	var tempArr = eval(whichArray(inputColor)); // scary setting tempArr to the right initial
	for (var i = 0; i < done.length; i++)
	{// remove all classes from done elements, add input color
		var param = 'pieces done ' + inputColor;
		alert(param);
		if ($(done[i]).hasClass('done'))
		{// element is in done and now has new color and 'pieces' 'done' and 'color'
			$(done[i]).removeClass();
			$(done[i]).addClass(param);
		}
	}
	
	for (var j = 0; j < tempArr.length; j++)
	{// tempArr[j] is like 'p00'
		for (var s = 0; s < done.length; s++)
		{// done[s] is like '#p00'
			if (touches(done[s], tempArr[j]))
			{// element in initial color array touches element in done, join them
				if (!(contains(tempArr[j])))
				{// if the tempArr[j] is not in done already, add it.
					alert(1);
					done.push(tempArr[j]);
				}
			}
		}
	}
	updateDone();
	alert(done);
}
function contains(element)
{
	for (var i = 0; i < done.length; i++)
	{
		if (done[i] == element)
		{// done already contains this element
			return true;
		}
	}
	// done does not contain this element
	return false;
}
function touches(doneEle, initialEle)
{// doneEle = '#p00', initialEle = 'p00'
	var tempInit = initialEle.replace('#p', '');
	var tempx = tempInit[0];
	var tempy = tempInit[1];
	var nexts = new Array();
	var param = '';
	if (tempy != 0)
	{// not on top row, check to north
		param = 'p' + tempx + String(parseInt(tempy) - 1);
		if (doneEle.search(param) != -1)
		{
			return true;
		}
	}
	if (tempx != gamesize-1)
	{// not on far right, check to east
		param = 'p' + String(parseInt(tempx) + 1) + tempy;
		if (doneEle.search(param) != -1)
		{
			return true;
		}
	}
	if (tempy != gamesize-1)
	{// not on bottom row, check to south
		param = 'p' + tempx + String(parseInt(tempy) + 1);
		if (doneEle.search(param) != -1)
		{
			return true;
		}
	}
	if (tempx != 0)
	{// not on far left, check to west
		param = 'p' + String(parseInt(tempx) - 1) + tempy;
		if (doneEle.search(param) != -1)
		{
			return true;
		}
	}
	return false;
}
function whichArray(inputColor)
{
	switch(inputColor)
	{
		case 'blue':
			return 'blueInitial';
			break;
		case 'red':
			return 'redInitial';
			break;
		case 'orange':
			return 'orangeInitial';
			break;
		case 'yellow':
			return 'yellowInitial';
			break;
		case 'purple':
			return 'purpleInitial';
			break;
		case 'green':
			return 'greenInitial';
			break;
		
		default:
			return true;
	}
}
var done = new Array();
done.push('#p00');
function updateDone()
{
	for (var j = 0; j < done.length; j++)
	{// each element in done array
		if (!($(done[j]).hasClass('done')))
		{// if element doesnt have 'done' class, add it.
			$(done[j]).addClass('done');
		}
	}

}
function piecesaround(pieceId)
{
	alert(pieceId);
	//id looks like 'p01''pxy'
	var tmpId = pieceId.replace('p', '');
	var xindex = tmpId[0];
	var yindex = tmpId[1];
	var arr = getnextids(pieceId);
	for (var i = 0; i < arr.length; i++)
	{
	}
	if (yindex == 0)
	{
		// on top row
		if (xindex == 0)
		{
			//no left
		}
		else if (xindex == gamesize-1)
		{
			//no right
		}
	}
	else if (yindex == gamesize-1)
	{
		// on bottom row
		if (xindex == 0)
		{
			//no left
		}
		else if (xindex == gamesize-1)
		{
			//no right
		}
	}
	else if (xindex == 0)
	{
		//dont check to left
		alert('dont check to my left');
	}
	else if (xindex == gamesize-1)
	{
		//dont check to my right
		alert('dont check to my right');
	}
	else
	{
		//check everybody around me
		alert('last case');
	}
}
